package handler

import (
	"common"
	"common/biz"
	"common/logs"
	"common/utils"
	"context"
	"core/models/db"
	"core/repo"
	"core/service"
	"encoding/json"
	"framework/msError"
	"framework/remote"
	"hall/component/proto"
	"hall/logic"
	"hall/models/request"
	"hall/models/response"
	"strconv"
	"strings"
)

type UserHandler struct {
	newsService      *service.NewsService
	marketService    *service.MarketService
	userService      *service.UserService
	warehouseService *service.WarehouseService
	goodsService     *service.GoodsService
	walletService    *service.WalletService
	nftService       *service.NFTService
	gm               *logic.GoodsManager
	base             *BaseHandler
	marketM          *logic.MarketManager
	vipM             *logic.VipManager
}

func (h *UserHandler) UpdateUserAddress(session *remote.Session, msg []byte) any {
	logs.Info("UpdateUserAddress msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		return common.F(biz.InvalidUsers)
	}
	//ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	//defer cancel()

	ctx := context.TODO()
	user := h.userService.GetUserOsReal(ctx, uint(uid))
	if user.RoleID == 0 {
		return common.F(biz.InvalidUsers)
	}
	if user.RealId == 0 {
		return common.F(biz.RealError)
	}
	err := new(msError.Error)
	var hfResult string
	var code int64
	var errs error
	if user.WalletId == 0 {
		//未开通钱包
		wallet := new(db.UserHfWallet)
		wallet, err = h.walletService.VerificationWallet(ctx, user)
		if err != nil {
			return common.F(err)
		}
		user.Wallet = *wallet
		if wallet.ID == 0 {
			hfResult, code, err = h.walletService.OpenWallet(ctx, user)
			if err != nil {
				return common.F(err)
			}
		}
	} else {
		hfResult, code, errs = h.walletService.BalanceWallet(user.Wallet.UserCustID, user.Wallet.AcctID)
		if errs != nil {
			return common.F(biz.SqlError)
		}
	}
	return common.S(map[string]any{
		"code": code,
		"data": hfResult,
	})
}

func (h *UserHandler) RealName(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] RealName msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] RealName err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.UserRealName
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[userHandler]用户%d RealName err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}

	errs := h.userService.RealName(context.TODO(), uint(uid), &req)
	if errs != nil {
		return common.F(errs)
	}
	return common.S(nil)
}

func (h *UserHandler) WalletInfo(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] WalletInfo msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] WalletInfo err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	user := h.userService.GetUserOsReal(context.TODO(), uint(uid))
	if user.RoleID == 0 {
		logs.Error("[userHandler]用户%d WalletInfo err:未获取到用户数据 req=%v", uid, user)
		return common.F(biz.InvalidUsers)
	}
	if user.RealId == 0 {
		logs.Error("[userHandler]用户%d WalletInfo err:用户未实名 req=%v", uid, user)
		return common.F(biz.RealError)
	}
	err := new(msError.Error)
	var hfResult string
	var code int64
	var errs error
	if user.WalletId == 0 {
		//未开通钱包
		wallet := new(db.UserHfWallet)
		wallet, err = h.walletService.VerificationWallet(context.TODO(), user)
		if err != nil {
			return common.F(err)
		}
		user.Wallet = *wallet
		if wallet.ID == 0 {
			hfResult, code, err = h.walletService.OpenWallet(context.TODO(), user)
			if err != nil {
				return common.F(err)
			}
		}
	} else {
		hfResult, code, errs = h.walletService.BalanceWallet(user.Wallet.UserCustID, user.Wallet.AcctID)
		if errs != nil {
			return common.F(biz.SqlError)
		}
	}
	if code == 605 || (code == 603 && user.OperationID == nil) {
		//生成链地址
		go h.nftService.CreateAccount(context.TODO(), uint(uid))
	}
	return common.S(map[string]any{
		"code": code,
		"data": hfResult,
	})
}

func (h *UserHandler) PullActivityInfo(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] PullActivityInfo msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] PullActivityInfo err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	var req request.PageMsg
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[userHandler]用户%d PullActivityInfo err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	ctx := context.TODO()
	peopleNum, data := h.userService.GetRole(ctx, uint(uid), 1)
	mineral := h.warehouseService.WarehouseMaterial(ctx, uint(uid), 11)
	total := h.userService.GetLastUserRewardSum(ctx, uint(uid))
	count, list := h.userService.GetLastUserReward(ctx, uint(uid), &req)
	return common.S(map[string]any{
		"mineral":    mineral.Num,
		"people_num": peopleNum,
		"level":      data.LevelID,
		"total":      total,
		"list": response.CommonList{
			Count: count,
			List:  list,
		},
	})
}

func (h *UserHandler) PullActivityReceive(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] PullActivityReceive msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] PullActivityReceive err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	ctx := context.TODO()
	err := h.userService.PullActivityReceive(ctx, uint(uid))
	if err != nil {
		return common.F(err)
	}
	return common.S(nil)
}

func (h *UserHandler) PullActivityContribution(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] PullActivityContribution msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] PullActivityContribution err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.UserContribute
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[userHandler]用户%d PullActivityContribution err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	total, count, list := h.userService.PullActivityContribution(ctx, uint(uid), &req)
	mineral := h.warehouseService.WarehouseMaterial(ctx, uint(uid), 11)
	return common.S(map[string]any{
		"total":   total,
		"mineral": mineral.Num,
		"data": response.CommonList{
			Count: count,
			List:  list,
		},
	})
}

func (h *UserHandler) PullActivityContributionReceive(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] PullActivityContributionReceive msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] PullActivityContributionReceive err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.CommonType
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[userHandler]用户%d PullActivityContribution err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	err := h.userService.PullActivityContributionReceive(ctx, uint(uid), req.Type)
	if err != nil {
		return common.F(err)
	}
	return common.S(nil)
}

func (h *UserHandler) PullActivityFriends(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] PullActivityFriends msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] PullActivityFriends err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.PageMsg
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[userHandler]用户%d PullActivityFriends err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	count, list := h.userService.PullActivityFriends(ctx, uint(uid), &req)
	return common.S(response.CommonList{
		Count: count,
		List:  list,
	})
}

func (h *UserHandler) ContributeLog(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] ContributeLog msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] ContributeLog err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.UserContributeLog
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[userHandler]用户%d RealName err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	//ctx := context.TODO()
	//count, list := h.userService.ContributeLog(ctx, uint(uid), &req)
	//return common.S(response.CommonList{
	//	Count: count,
	//	List:  list,
	//})
	return common.S(nil)
}

func (h *UserHandler) Activity(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] Activity msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] Activity err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	ctx := context.TODO()
	list := h.userService.ActivityList(ctx, uint(uid))
	return common.S(list)
}

func (h *UserHandler) ActivityReceive(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] ActivityReceive msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] ActivityReceive err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	ctx := context.TODO()
	var req request.ActivityReceive
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[userHandler]用户%d ActivityReceive err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	err := h.userService.ActivityReceive(ctx, uint(uid), &req)
	if err != nil {
		return common.F(err)
	}
	return common.S(nil)
}

func (h *UserHandler) ContributeReceive(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] ContributeReceive msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] ContributeReceive err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	ctx := context.TODO()
	var req request.ContributeReceive
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[userHandler]用户%d ContributeReceive err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	err := h.userService.ContributeReceive(ctx, uint(uid), &req)
	if err != nil {
		return common.F(err)
	}
	return common.S(nil)
}

// UserCancel 用户注销
func (h *UserHandler) UserCancel(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] UserCancel msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] UserCancel err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	ctx := context.TODO()
	user := h.userService.GetUserOsReal(ctx, uint(uid))
	if user.ID == 0 {
		logs.Error("[userHandler]用户%d UserCancel err:获取用户数据失败", uid)
		return common.F(biz.InvalidUsers)
	}
	err := h.userService.UpdateUserCancel(ctx, uid, 9)
	if err != nil {
		logs.Error("[userHandler]用户%d UserCancel err:注销失败 dbErr=%v", uid, err)
		return common.F(biz.UserClenError)
	}
	return common.S(nil)
}

func (h *UserHandler) WalletMy(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] WalletMy msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] WalletMy err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	ctx := context.TODO()
	user := h.userService.GetUserOsReal(ctx, uint(uid))
	if user == nil {
		logs.Error("[userHandler]用户%d WalletMy err:获取用户数据失败", uid)
		return common.F(biz.SqlError)
	}
	if user.WalletId == 0 {
		logs.Error("[userHandler]用户%d WalletMy err:未开通钱包", uid)
		return common.F(biz.WalletError)
	}
	hfResult, err := h.walletService.MyWallet(ctx, user.Wallet.UserCustID)
	if err != nil {
		logs.Error("[userHandler]用户%d WalletMy err:获取地址失败", uid)
		return common.F(biz.SqlError)
	}
	return common.S(hfResult)
}

// Friends 好友列表
func (h *UserHandler) Friends(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] Friends msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] Friends err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	//ctx := context.TODO()
	//data := h.userService.UserFriends(ctx, uint(uid))
	//return common.S(data)
	return common.S(nil)
}

func (h *UserHandler) MonthlyCard(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] MonthlyCard msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] MonthlyCard err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	ctx := context.TODO()
	var req request.CommonID
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[userHandler]用户%d ContributeReceive err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	data, err := h.userService.MonthlyCard(ctx, uint(uid), req.ID)
	if err != nil {
		return common.F(err)
	}
	if h.vipM.Session[uint(uid)] == nil {
		h.vipM.Session[uint(uid)] = session
	}
	return common.S(data)
}

func (h *UserHandler) MonthlyCardOpen(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] MonthlyCard msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] MonthlyCard err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.CommonID
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[userHandler]用户%d ContributeReceive err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	order, err := h.vipM.MonthlyCardOpen(ctx, uint(uid), req.ID, session)
	if err != nil {
		return common.F(err)
	}
	return common.S(order)
}

func (h *UserHandler) MonthlyCardPay(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] MonthlyCard msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] MonthlyCard err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.GoodsOrderNo
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[Goods]用户%d Pay err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}

	//userData, err := h.gm.GetGoodsUser(uint(uid), h.userService)
	//if err != nil {
	//	return common.F(err)
	//}
	//// 数据检验
	//err = h.base.Check(userData)
	//if err != nil {
	//	return common.F(err)
	//}
	//payUrl, err := h.vipM.OrderPay(uint(uid), req.OrderNo)
	//if err != nil {
	//	return common.F(err)
	//}
	//return common.S(map[string]any{
	//	"payUrl": payUrl,
	//})
	return common.S(nil)
}

func (h *UserHandler) MonthlyCardReceive(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] MonthlyCard msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] MonthlyCard err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.CommonID
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[userHandler]用户%d ContributeReceive err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	err := h.vipM.Receive(uint(uid), req.ID, session)
	if err != nil {
		return common.F(err)
	}
	return common.S(nil)
}

func (h *UserHandler) SignInfo(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] SignInfo msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] SignInfo err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.CommonID
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[userHandler]用户%d ContributeReceive err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	data, err := h.userService.MonthlyCard(ctx, uint(uid), req.ID)
	if err != nil {
		return common.F(err)
	}
	if h.vipM.Session[uint(uid)] == nil {
		h.vipM.Session[uint(uid)] = session
	}
	sign, receive := h.userService.SignInfo(ctx, uint(uid), req.ID)
	ks := h.warehouseService.WarehouseMaterial(ctx, uint(uid), 11)
	jb := h.warehouseService.WarehouseMaterial(ctx, uint(uid), 12)
	equity := strings.Split(data.Info, "\n")

	return common.S(map[string]any{
		"isReceive": receive,
		"mineral":   ks.Num,
		"equity":    equity,
		"gold":      jb.Num,
		"sign":      sign,
		"monthly":   data,
	})
}

func (h *UserHandler) UserSign(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] UserSign msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] UserSign err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.CommonID
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[userHandler]用户%d UserSign err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	//err := h.userService.UserSign(ctx, uint(uid), req.ID)
	err := h.vipM.SignReceive(ctx, uint(uid), req.ID, session)
	if err != nil {
		return common.F(err)
	}
	return common.S(nil)
}

func (h *UserHandler) CheckUser(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] CheckUser msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] CheckUser err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.CommonID
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[userHandler]用户%d CheckUser err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	res, err := h.userService.CheckUser(ctx, uint(uid), int64(req.ID))
	if err != nil {
		return common.F(err)
	}
	return common.S(res)
}

func (h *UserHandler) Chamber(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] Chamber msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] Chamber err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.PageMsg
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[userHandler]用户%d Chamber err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	count, list := h.userService.ChamberList(ctx, req.Page, req.PageSize)
	return common.S(response.CommonList{
		Count: count,
		List:  list,
	})
}

func (h *UserHandler) ChamberMember(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] ChamberMember msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] ChamberMember err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.ChamberMember
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[userHandler]用户%d ChamberMember err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	count, list := h.userService.ChamberMember(ctx, req.ID, req.PageMsg)
	return common.S(response.CommonList{
		Count: count,
		List:  list,
	})
}

func (h *UserHandler) LotteryList(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] LotteryList msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] LotteryList err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	ctx := context.TODO()
	lottery := h.userService.GetLotteryList(ctx)
	mineral := h.warehouseService.WarehouseMaterial(ctx, uint(uid), 11)
	consumeTypeIDStr := h.userService.GetConfInfo(ctx, "lottery_consume_type_id")
	consumeTypeID, _ := strconv.Atoi(consumeTypeIDStr)
	types := h.userService.GetTypeInfo(ctx, uint(consumeTypeID))
	consumeNumStr := h.userService.GetConfInfo(ctx, "lottery_consume_num")
	consumeNumInt, _ := strconv.Atoi(consumeNumStr)
	consumeNum := int64(consumeNumInt)
	if consumeTypeID == 11 {
		consumeNum = utils.DecimalFloatByInt(utils.DecimalIntDiv(consumeNum, types.Val))
	}
	desc := h.userService.GetConfInfo(ctx, "lottery_desc")
	log := h.userService.LotteryLog(ctx, 10)
	return common.S(map[string]any{
		"lottery":         lottery,
		"consumeTypeID":   consumeTypeID,
		"consumeTypeName": types.Name,
		"consumeTypeImg":  types.Img,
		"consumeNum":      consumeNum,
		"mineral":         mineral.Num,
		"desc":            desc,
		"log":             log,
	})
}

func (h *UserHandler) Lottery(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] Lottery msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] Lottery err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	ctx := context.TODO()
	data, err := h.userService.Lottery(ctx, uint(uid))
	if err != nil {
		return common.F(err)
	}
	mineral := h.warehouseService.WarehouseMaterial(ctx, uint(uid), 11)
	log := h.userService.LotteryLog(ctx, 10)
	return common.S(map[string]any{
		"lottery": data,
		"mineral": mineral.Num,
		"log":     log,
	})
}

func (h *UserHandler) Customer(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] Customer msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] Customer err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	ctx := context.TODO()
	data := h.userService.Customer(ctx)
	return common.S(data)
}

func (h *UserHandler) SetTransactionPassword(session *remote.Session, msg []byte) any {
	logs.Info("[userHandler] SetTransactionPassword msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[userHandler] SetTransactionPassword err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.SetPwd
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[userHandler]用户%d SetTransactionPassword err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	user, err := h.userService.SetTransactionPassword(ctx, uint(uid), &req)
	if err != nil {
		return common.F(err)
	}
	session.Push([]string{session.GetUid()}, proto.UserNotifyData(user), "ServerMessagePush")
	return common.S(nil)
}

func NewUserHandler(r *repo.Manager, gm *logic.GoodsManager, marketM *logic.MarketManager, m *logic.VipManager) *UserHandler {
	return &UserHandler{
		newsService:      service.NewNewsService(r),
		marketService:    service.NewMarketService(r),
		userService:      service.NewUserService(r),
		warehouseService: service.NewWarehouseService(r),
		goodsService:     service.NewGoodsService(r),
		walletService:    service.NewWalletService(r),
		nftService:       service.NewNFTService(r),
		base:             NewBaseHandler(r),
		gm:               gm,
		marketM:          marketM,
		vipM:             m,
	}
}
